const conn = require('../db/config')

//判断用户是管理员还是普通用户还是非系统用户
exports.check = (req,res) => {
    const username = "'"+req.query.username+"'"
    const password = req.query.pass
    conn.query("SELECT * FROM `adminuser` WHERE name = " + username,(err,result) => {
        if(err) return console.log(err)
        if(result.length){
            var string = JSON.stringify(result);
			var json = JSON.parse(string)[0];
			if (json.password == password) {
				res.send({checkmsg:'该用户是管理员',type:'admin',username:json.name});
			} else {
                res.send({checkerr:'密码错误',errtype:'passerr'});
			}
        }else{
            conn.query('SELECT * FROM `user` WHERE name = ' + username,(err,result) => {
                if(err) return console.log(err)
                if(result.length){
                  var string = JSON.stringify(result);
			      var json = JSON.parse(string)[0];
                  if (json.password == password) {
                    res.send({checkmsg:'该用户是普通用户',type:'user',username:json.name});
                }else{
                    res.send({checkerr:'密码错误',errtype:'passerr'});
                }
                }
                else{
                    res.send({checkerr:'用户不存在',errtype:'usererr'})
                }
        })
        }
    })
}